<template>
	<view>
		<cu-custom :bgColor="globalData.bgColor" :isBack="true">
			<template #content>
				<text class="text-darkgrey">{{ $t('account.index.hc2hw6') }}</text>
			</template>
		</cu-custom>
		<form ref="resetPasswordForm">
			<view>
				<view class="cu-form-group solid-top">
					<input name="inputOldPassword" v-model="form.password" prop="password" :placeholder="$t('account.reset-password.s75263')" :maxlength="16" type="password" />
				</view>
			</view>
			<view>
				<view class="cu-form-group solid-top">
					<input name="inputNewPassword" v-model="form.newpassword1" prop="newpassword1" :placeholder="$t('login.forget.hxihwy')" :maxlength="16" type="password" />
				</view>
			</view>
			<view>
				<view class="cu-form-group solid-top">
					<input
						name="confirmNewPassword"
						v-model="form.newpassword2"
						prop="newpassword2"
						:placeholder="$t('account.reset-password.191v5y')"
						:maxlength="16"
						type="password"
					/>
				</view>
			</view>
			<view class="padding-sm text-sm text-purple-grey">
				<text class="cuIcon-info margin-right-xs"></text>
				{{ $t('account.reset-password.l2p316') }}
			</view>
		</form>
		<view class="padding">
			<button class="cu-btn block bg-blue margin-tb-sm lg shadow-blur round" @click="submitForm">{{ $t('order.logisticsform.tc3h47') }}</button>
		</view>
	</view>
</template>

<script>
import { editObj, bindPhone, getObj } from '@/api/upms/user';
import service from '@/store/service'; //存储相关数据服务util

export default {
	components: {},
	computed: {},
	onLoad() {
		let user = service.getUser();
		this.form.username = user.username;
		this.form.avatar = user.avatar;
		this.form.email = user.email;
		this.form.id = user.id;
		this.form.organId = user.organId;
	},
	data() {
		return {
			globalData: getApp().globalData,
			chosen: '',
			timerId: null,
			form: {
				username: '',
				password: '',
				newpassword1: '',
				newpassword2: '',
				avatar: '',
				email: '',
				id: '',
				organId: ''
			}
		};
	},
	methods: {
		submitForm: function (e) {
			var that = this;
			if (this.validate()) {
				editObj(this.form)
					.then((response) => {
						if (response.data) {
							uni.showToast({
								title: this.$t('account.reset-password.43u8eo')
							});
							service.removeRefreshToken();
							uni.reLaunch({
								url: '/pages/login/login'
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: this.$t('account.index.s1ro1w') + response.data
							});
						}
					})
					.catch(() => {
						uni.showToast({
							icon: 'none',
							title: this.$t('account.index.s1ro1w')
						});
					});
			}
		},

		validate() {
			//验证
			let validate = true;
			let names = [
				{ key: 'newpassword2', label: this.$t('account.reset-password.t50g35') },
				{ key: 'newpassword1', label: this.$t('account.reset-password.1uys14') },
				{ key: 'password', label: this.$t('account.reset-password.4tc6v4') }
			];
			names.map((item) => {
				if (!this.form[item.key]) {
					validate = false;
					uni.showToast({
						icon: 'none',
						title: item.label + this.$t('order.logisticsform.qw7448')
					});
					return;
				}
			});
			if (validate && this.form.newpassword1.length < 6) {
				validate = false;
				uni.showToast({
					icon: 'none',
					title: this.$t('account.reset-password.6rcvtt')
				});
				return;
			}
			if (validate && this.form.newpassword1 != this.form.newpassword2) {
				validate = false;
				uni.showToast({
					icon: 'none',
					title: this.$t('account.reset-password.044l76')
				});
				return;
			}
			return validate;
		}
	}
};
</script>

<style scoped></style>
